/*
* Copyright (c) 2016 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
*/
package com.hyyt.shopjsp.util.common;

import org.apache.commons.lang.StringUtils;

import java.util.*;
import java.util.regex.Pattern;

/**
 * @author Administrator on 2016/7/21 0021.
 */
public class SortUtil {

    /**
     * 商品某些排序字段
     * @param sort ，从前台拿到的统一参数，
     *                defaults默认，totalSale-desc销量降序，salesPrice-asc价格升序，salesPrice-desc价格降序，timeDesc时间降序
     *                "totalSale-desc,salesPrice-asc"
     * @return
     */
    public static Map<String,Object> toSort(String sort){
        Map<String, Object> map = new HashMap<>();
        if (sort!=null&&!"".equals(sort)) {
            StringBuffer sortName = new StringBuffer();
            String[] strs = {};
            List<String> arr = new ArrayList(Arrays.asList(sort.split(",")));
            if (arr.size() > 0) {
                for (int i = 0; i < arr.size(); i++) {
                    if (arr.get(i).indexOf("-") != -1) {//包含-
                        strs = arr.get(i).split(Pattern.quote("-"));
                        map.put(strs[0], strs[0] + " " + strs[1]);
                    } else {//不包含"/",统一规范参数会影响蔡昂前台样式
                    switch (arr.get(i)){
                        case "totalSalesAsc" : map.put("totalSales","totalSales asc"); break;
                        case "totalSales" : map.put("totalSales","totalSales desc"); break;
                        case "putSaleDateDesc" : map.put("putSaleDate","putSaleDate desc"); break;
                        case "salesPriceAsc" : map.put("salesPrice","salesPrice asc"); break;
                        case "salesPriceDesc" : map.put("salesPrice","salesPrice desc"); break;
                        case "productIdDesc" : map.put("productId","productId desc"); break;
                    }
                       // map.put(arr.get(i), arr.get(i) + "asc");
                    }
                }
            }
        }
     // String x=  orderBy.replaceAll("-"," ");
        return map;

       /* if(Utils.stringIsNotEmpty(orderBy)){
            if("defaults".equals(orderBy)){//默认
                sortName="ShopHomeDayrecommend.sort asc";
            }else if("totalSaleDesc".equals(orderBy)){//销量的降序
                sortName="ShopProductinfo.totalSales desc";
            }else if("priceAsc".equals(orderBy)){//价格升序
                sortName="ShopProductinfo.salesPrice asc";
            }else if("timeDesc".equals(orderBy)){//时间的降序
                sortName="ShopProductinfo.putSaleDate desc";
            }else if("priceDesc".equals(orderBy)){//价格降序
                sortName="ShopProductinfo.salesPrice desc";
            }*/



    }

    /**
     * 首页商品列表排序
     * @param orderBy
     * @return
     */
    public static String toOrderBy(String orderBy){
        String order_by = "productId desc";
        if (StringUtils.isNotEmpty(orderBy)) {//判断根据什么排序（默认为根据商品id排序）
            switch (orderBy) {
                case "normal":
                    order_by = "productId desc";//初始化进入商品图片（列表）
                    break;
                case "salesPriceAsc":
                    order_by = "salesPrice asc";//3:排序：（按价格排序）：salesPrice升序   Price
                    break;
                case "salesPriceDesc":
                    order_by = "salesPrice desc";//4:排序：（按价格排序）：salesPrice降序   Price
                    break;
                case "putSaleDate":
                    order_by = "putSaleDate desc";//6:排序（商品上架时间降序）：putSaleDate  Latest
                    break;
                case "totalSales":
                    order_by = "totalSales desc";//7:排序（商品销售量降序）：totalSales     Popular
                    break;
            }
        }
        return order_by;
    }

    /**
     * 商家商品列表排序
     * @param orderBy
     * @return
     */
    public static String shopOrderBy(String orderBy){
        String order_by = "putSaleDate desc";
        if (StringUtils.isNotEmpty(orderBy)) {//判断根据什么排序（默认为根据商品id排序）
            switch (orderBy) {
                case "Latest":
                    order_by = "putSaleDate desc";//初始化进入商品图片（列表）上架时间降序
                    break;
                case "salesPrice-asc":
                    order_by = "salesPrice asc";//3:排序：（按价格排序）：salesPrice升序   Price
                    break;
                case "salesPrice-desc":
                    order_by = "salesPrice desc";//4:排序：（按价格排序）：salesPrice降序   Price
                    break;
                case "totalSales-desc":
                    order_by = "totalSales desc";//6:排序（商品销售量升序）：putSaleDate  Latest
                    break;
                case "totalSales-asc":
                    order_by = "totalSales asc";//7:排序（商品销售量降序）：totalSales     Popular
                    break;
            }
        }
        return order_by;
    }

}
